package com.aierkeji.call.b2c.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.handler.RequestMatchResult;

import com.aierkeji.call.b2c.entity.AkjCallVersion;
import com.aierkeji.call.b2c.service.AkjCallVersionService;
import com.aierkeji.call.b2c.util.GsonUtils;
import com.aierkeji.call.b2c.util.ResultMapHelper;

@Component
public class VersionFilter extends OncePerRequestFilter {
	private Logger logger = Logger.getLogger(getClass());

	private String[] urls = { "/login/login"};
	@Autowired
	private AkjCallVersionService akjCallVersionService;

	
	
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		String versionName = httpServletRequest.getHeader("versionName");
		AkjCallVersion akjCallVersion = akjCallVersionService.getVersion();
		if(!akjCallVersion.getVersion().equals(versionName)&& !akjCallVersion.getOtherVersionUse()) {
			if(updateApp(versionName,akjCallVersion.getVersion())) {
				Map<String,Object> map = ResultMapHelper.result("90","当前版本已暂停使用，请到"+akjCallVersion.getDownLoadUrl()+"下载最新版本。");
				String key = GsonUtils.parseToJson(map);
				httpServletResponse.getWriter().write(key);
				httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
				return ;
			}
		}
		filterChain.doFilter(httpServletRequest, httpServletResponse);
	}

	@Override
	protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
		String url = request.getRequestURI();
		String pathInfo = request.getServletPath();
		for (String key : urls) {
			if (PatternMatchUtils.simpleMatch(key, pathInfo)) {
				return false;
			}
		}

		return true;
	}
	
	public static  boolean updateApp(String localVersion, String newVersion) {
	    String[] localVersionArray = localVersion.split("\\.");
	    String[] newVersionArray = newVersion.split("\\.");
	    if (localVersionArray.length < newVersionArray.length) {
	        int cha = newVersionArray.length - localVersionArray.length;
	        for (int i = 0; i < cha; i++) {
	            localVersion = localVersion + ".0";
	        }
	        localVersionArray = localVersion.split("\\.");
	    }
	    try {
	        for (int i = 0; i < newVersionArray.length; i++) {
	            int temp = Integer.parseInt(newVersionArray[i]);
	            int compar = Integer.parseInt(localVersionArray[i]);
	            if (temp > compar) {
	                return true;
	            }
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return false;
	}

}
